//
//  NMSceneTableViewCell.m
//  NotificationMe
//
//  Created by max on 2018/12/16.
//  Copyright © 2018年 dayang. All rights reserved.
//

#import "NMSceneTableViewCell.h"
#import <MapKit/MapKit.h>

NSString *kNMSceneTableViewCell = @"NMSceneTableViewCell";

@interface NMSceneTableViewCell () <MKMapViewDelegate,CLLocationManagerDelegate>


@property (nonatomic, strong) MKMapView *mapView;

@property (nonatomic, strong) MKPointAnnotation *pointAnnotation;

@end

@implementation NMSceneTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    return self;
}

#pragma mark - Private Methods
- (void)setupViews {
    self.contentView.backgroundColor = UIColor.whiteColor;
   
}

- (void)setStyle:(NMSceneStyle)model; {
    switch (model) {
        case NMSceneStyleDateAndTime: {
            [self.contentView addSubview:self.picker];
            _picker.datePickerMode = UIDatePickerModeDateAndTime;
            [_picker mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
        }
            break;
        case NMSceneStyleCountDownTimer:{
            [self.contentView addSubview:self.picker];
            _picker.datePickerMode = UIDatePickerModeCountDownTimer;
            [_picker mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
        } break;
        case NMSceneStyleMap: {
            [self.contentView addSubview:self.mapView];
            [_mapView setUserTrackingMode:MKUserTrackingModeFollow];
            self.mapView.delegate = self;
            [_mapView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            
            MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake([NMCLLocationManager shared].location.coordinate.latitude, [NMCLLocationManager shared].location.coordinate.longitude), MKCoordinateSpanMake(0.05, 0.05));
            [_mapView setRegion:region animated:false];
            if ([NMCLLocationManager shared].selectionCllocation.latitude) {
                _mapView.centerCoordinate = [NMCLLocationManager shared].selectionCllocation;
                self.pointAnnotation.coordinate =   [NMCLLocationManager shared].selectionCllocation;
                [self.mapView addAnnotation:self.pointAnnotation];
                
//                MKCircle *circle = [MKCircle circleWithCenterCoordinate:[NMCLLocationManager shared].selectionCllocation radius:800];
//                [self.mapView addOverlay:circle];
            }
            
        } break;
    }
}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id)annotation {
    MKPinAnnotationView *pinView = nil;
    if(annotation != self.mapView.userLocation) {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if (pinView == nil) {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
            pinView.animatesDrop = false;
            pinView.canShowCallout = false;
        }
    }
    return pinView;
}

//- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
//    MKCircleRenderer *renderer = [[MKCircleRenderer alloc] initWithOverlay:overlay];
//    renderer.fillColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
//    renderer.strokeColor = [UIColor blueColor];
//    renderer.lineWidth = 2;
//    return renderer;
//}

+ (CGFloat)height {
    return 200;
}

- (UIDatePicker *)picker {
    if (!_picker) {
        _picker = [[UIDatePicker alloc] init];
    }
    return _picker;
}


- (MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc] init];
        _mapView.userInteractionEnabled = false;
    }
    return _mapView;
}

- (MKPointAnnotation *)pointAnnotation {
    if (!_pointAnnotation) {
        _pointAnnotation = [[MKPointAnnotation alloc] init];
    }
    return _pointAnnotation;
}


@end













